package main
import(
	"fmt"
)
func buildNum(intChan chan int){
	for i := 1; i <= 200000; i++{
		intChan<- i
	}
	close(intChan)
}

func readNum(intChan chan int,resChan chan map[int]int,exitChan chan bool){
	var rc map[int]int
	for {
		v,ok := <- intChan
		if !ok {
			if len(resChan) == 200000 {
				exitChan<- true
				close(resChan)
			}
			break
		}
		var res int
		for i := 1 ; i <= v; i++{
			res += i
		}
		rc = map[int]int{v:res}
		resChan<- rc
	}
}

func main(){
	intChan := make(chan int ,50)
	resChan := make(chan map[int]int,200000)
	exitChan := make(chan bool ,1)
	go buildNum(intChan)
	for i := 1; i <= 8 ; i++{
		go readNum(intChan,resChan,exitChan)
	}
	//匿名协程
	//go func(){
		for{
			v := <-exitChan
			if v {
				for vs := range resChan {
					for z,s := range vs{
						fmt.Printf("res[%d]=%v\n",z,s)
					}
				}
				break
			}
		}
	//}()
}
